import GbzResManager from "../manager/GbzResManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzTarget extends cc.Component {

    screwNumMax: number = 5
    screwArr: number[] = [0, 0, 0, 0, 0, 0, 0]

    isActive() {
        let active = false
        for (let i = 0; i < this.screwNumMax; i++) {
            if (!this.screwArr[i]) {
                active = true
                break
            }
        }
        return active
    }

    addScrew(index: number, targetScrew: cc.Node) {
        targetScrew.scale = 1
        targetScrew.getComponent(cc.Sprite).spriteFrame = GbzResManager.ins.sprite(`gbz_screw${index}`)
        targetScrew.active = true
    }

    clearAll() {
        this.screwArr = [0, 0, 0, 0, 0, 0, 0]
        for (let i = 0; i < this.node.children.length; i++) {
            const target = this.node.children[i]
            const screw = target.getChildByName('screw')
            screw.active = false
        }
    }

    clearOne(targetScrew: cc.Node) {
        const i = this.node.children.findIndex(target => (target.getChildByName('screw')) == targetScrew)
        this.screwArr[i] = 0
        targetScrew.active = false
    }

    getLock() {
        let targetLock = null
        for (let i = 0; i < this.node.children.length; i++) {
            const target = this.node.children[i]
            if (target.getChildByName('lock')) {
                targetLock = target.getChildByName('lock')
                break
            }
        }
        return targetLock
    }

    pushScrew(index: number) {
        for (let i = 0; i < this.screwNumMax; i++) {
            if (!this.screwArr[i]) {
                this.screwArr[i] = index
                break
            }
        }
    }

    hasScrew() {
        const screws = this.getScrews()
        return screws.length
    }

    getScrews() {
        let screwArr = []
        for (let i = 0; i < this.screwNumMax; i++) {
            const target = this.node.children[i]
            const screw = target.getChildByName('screw')
            if (screw.active) screwArr.push(screw)
        }
        return screwArr
    }

    getScrewsReal() {
        let screwArr = []
        for (let i = 0; i < this.screwNumMax; i++) {
            if (this.screwArr[i]) {
                const target = this.node.children[i]
                const screw = target.getChildByName('screw')
                screwArr.push(screw)
            }
        }
        return screwArr
    }

    addUnlockNum() {
        this.screwNumMax += 1
    }

    getScrewCurrent() {
        let targetScrew = null
        const index = this.screwArr.findIndex(screw => !screw)
        if (index >= 0) {
            const target = this.node.children[index]
            const screw = target.getChildByName('screw')
            targetScrew = screw
        }
        return targetScrew
    }

    checkFull() {
        let isFull = true
        for (let i = 0; i < this.screwNumMax; i++) {
            if (!this.screwArr[i]) {
                isFull = false
                break
            }
        }
        if (isFull) {
            for (let i = 0; i < this.screwNumMax; i++) {
                const target = this.node.children[i]
                const screw = target.getChildByName('screw')
                if (!screw.active) {
                    isFull = false
                    break
                }
            }
        }
        return isFull
    }
}
